HAL QSPI initialization and configuration functions migration

The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:

  • Open STM32CubeMX2 QSPI configuration panel.

  • Configure the SW layer HAL.

  • Configure the QSPI main features.

  • Configure the advanced QSPI features if needed.

  • Configure the QSPI GPIO pins.

  • Configure the QSPI DMA(s) if the application requires to use the QSPI in DMA mode.

  • Configure the NVIC if the application requires the QSPI to be used in IT mode.

  • Generate the code.

Note

In HAL2, the XSPI driver now allows addressing quad, octal, and hexa memories(No more QSPI driver in HAL2)

HAL QSPI configuration panel

Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

Project configuration panel

HAL QSPI main features configuration

Configure the QSPI main features.

QSPI main feature configuration

HAL QSPI advanced features configuration

Configure the advanced QSPI features if needed.

QSPI advanced feature configuration

HAL QSPI security and privilege features configuration

Configure the QSPI security and privilege features if needed.

QSPI security and privilege feature configuration

GPIO configuration

Configure GPIO pins.

Note

The GPIO configuration is managed in XSPIM

Main features configuration

QSPI GPIO configuration QSPI GPIO port main configuration

DMA configuration

Configure the DMA for data reception as needed.

QSPI DMA configuration

NVIC configuration

Configure the NVIC in case of an interrupt.

QSPI NVIC configuration

Generated QSPI initialization and de-initialization sequences

The QSPI initialization sequence is generated in the mx_xspi1_init function in mx_xspi1.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

Related concepts:

STM32CubeMX2 Generated : QSPI initialization and de-initialization sequences

Topic

HAL1 code snippet

HAL2 code snippet

Initialization and configuration sequence

static void MX_QUADSPI_Init(void)
{
hqspi.Instance = QUADSPI;
hqspi.Init.ClockPrescaler = 255;
hqspi.Init.FifoThreshold = 1;
hqspi.Init.SampleShifting = QSPI_SAMPLE_SHIFTING_NONE;
hqspi.Init.FlashSize = 1;
hqspi.Init.ChipSelectHighTime = QSPI_CS_HIGH_TIME_1_CYCLE;
hqspi.Init.ClockMode = QSPI_CLOCK_MODE_0;
hqspi.Init.FlashID = QSPI_FLASH_ID_1;
hqspi.Init.DualFlash = QSPI_DUALFLASH_DISABLE;
if (HAL_QSPI_Init(&hqspi) != HAL_OK)
{
    Error_Handler();
}
}














































void HAL_QSPI_MspInit(QSPI_HandleTypeDef* hqspi)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  if(hqspi->Instance==QUADSPI)
  {

  /** Initializes the peripherals clock*/
    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_QSPI;
    PeriphClkInitStruct.QspiClockSelection = RCC_QSPICLKSOURCE_D1HCLK;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
    {
      Error_Handler();
    }

    /* Peripheral clock enable */
    __HAL_RCC_QSPI_CLK_ENABLE();

    __HAL_RCC_GPIOC_CLK_ENABLE();
    __HAL_RCC_GPIOB_CLK_ENABLE();
    __HAL_RCC_GPIOF_CLK_ENABLE();
    __HAL_RCC_GPIOA_CLK_ENABLE();
    /**QUADSPI GPIO Configuration
    PB0    ------> QUADSPI_BK1_IO1
    PA2     ------> QUADSPI_BK1_NCS
    PB1     ------> QUADSPI_BK1_IO0
    PA7     ------> QUADSPI_BK1_IO2
    PA6     ------> QUADSPI_BK1_IO3
    PA3     ------> QUADSPI_CLK
    */

    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF10_QUADSPI;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_7|GPIO_PIN_6|GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF10_QUADSPI;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  }
}
hal_xspi_handle_t *mx_xspi1_init(void)
{
  hal_xspi_config_t xspi_config;
  hal_xspi_memory_config_t memory_config;
  hal_xspi_timing_config_t timing_config;
  uint32_t p_max_clock_phase;

  if (HAL_XSPI_Init(&hOCTOSPI1, HAL_OCTOSPI1) != HAL_OK)
  {
    return NULL;
  }

  HAL_RCC_OCTOSPI1_EnableClock();
  HAL_RCC_GPIOP_EnableClock();
  HAL_RCC_GPIOO_EnableClock();
  if (HAL_RCC_OCTOSPI_SetKernelClkSource(HAL_RCC_OCTOSPI_CLK_SRC_SYSCLK) != HAL_OK)
  {
    return NULL;
  }
  /* Main peripheral configuration */
  memory_config.mode = HAL_XSPI_MEMORY_SINGLE;
  memory_config.type = HAL_XSPI_MEMORY_TYPE_MICRON;
  memory_config.size_bit = HAL_XSPI_MEMORY_SIZE_16BIT;
  memory_config.wrap_size_byte = HAL_XSPI_WRAP_NOT_SUPPORTED;
  memory_config.clock_polarity = HAL_XSPI_CLOCK_LOW;
  memory_config.cs_boundary = HAL_XSPI_CS_BOUNDARY_NONE;
  timing_config.clk_prescaler = 0x1U;
  timing_config.shift = HAL_XSPI_SAMPLE_SHIFT_NONE;
  timing_config.hold = HAL_XSPI_DELAY_HOLD_NONE;
  timing_config.cs_high_time_cycle = 0x1U;
  timing_config.cs_release_time_cycle = 0x0U;
  timing_config.cs_refresh_time_cycle = 0x0U;
  timing_config.dlyb_state = HAL_XSPI_DLYB_ON;

  xspi_config.memory = memory_config;
  xspi_config.timing = timing_config;

  if (HAL_XSPI_SetConfig(&hOCTOSPI1, &xspi_config) != HAL_OK)
  {
    return NULL;
  }

  /* Delay block configuration */
  if (HAL_XSPI_DLYB_Enable(&hOCTOSPI1) != HAL_OK)
  {
    return NULL;
  }
  if (HAL_XSPI_DLYB_CalculateMaxClockPhase(&hOCTOSPI1, &p_max_clock_phase) != HAL_OK)
  {
    return NULL;
  }

  if (HAL_XSPI_DLYB_SetConfigDelay(&hOCTOSPI1, p_max_clock_phase) != HAL_OK)
  {
    return NULL;
  }

  return &hOCTOSPI1;
}


system_status_t mx_xspim_init(void)
{
  hal_xspi_xspim_config_t xspim_config;

  HAL_RCC_OCTOSPIM_EnableClock();

  xspim_config.port1.clk = HAL_XSPI_XSPIM_XSPI1;
  xspim_config.port1.dqs = HAL_XSPI_XSPIM_XSPI1;
  xspim_config.port1.ncs = HAL_XSPI_XSPIM_XSPI1;
  xspim_config.port1.io_low = HAL_XSPI_XSPIM_XSPI1_IO_LOW;
  xspim_config.port1.io_high = HAL_XSPI_XSPIM_XSPI1_IO_HIGH;

  xspim_config.port2.clk = HAL_XSPI_XSPIM_NONE;
  xspim_config.port2.dqs = HAL_XSPI_XSPIM_NONE;
  xspim_config.port2.ncs = HAL_XSPI_XSPIM_NONE;
  xspim_config.port2.io_low = HAL_XSPI_XSPIM_IO_NONE;
  xspim_config.port2.io_high = HAL_XSPI_XSPIM_IO_NONE;

  xspim_config.req_ack_cycle = 0x0UL;

  if (HAL_XSPI_XSPIM_SetConfig(&xspim_config) != HAL_OK)
  {
    return SYSTEM_PERIPHERAL_ERROR;
  }


  HAL_RCC_GPIOA_EnableClock();
  HAL_RCC_GPIOB_EnableClock();
  HAL_RCC_GPIOC_EnableClock();
  hal_gpio_config_t  gpio_config;

  /**
    OCTOSPIM GPIO Configuration
    PA3   --------->  OCTOSPIM_P1_CLK
    PA1   --------->  OCTOSPIM_P1_DQS
    PA2   --------->  OCTOSPIM_P1_NCS
    PA7   --------->  OCTOSPIM_P1_IO2
    PA6   --------->  OCTOSPIM_P1_IO3
    **/
  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_10;
  HAL_GPIO_Init(HAL_GPIOA, HAL_GPIO_PIN_3 | HAL_GPIO_PIN_1 | HAL_GPIO_PIN_2 | HAL_GPIO_PIN_7 | HAL_GPIO_PIN_6, &gpio_config);


  /**
    OCTOSPIM GPIO Configuration
    PB12  --------->  OCTOSPIM_P1_NCLK
    PB1   --------->  OCTOSPIM_P1_IO0
    PB0   --------->  OCTOSPIM_P1_IO1
    **/
  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_10;
  HAL_GPIO_Init(HAL_GPIOB, HAL_GPIO_PIN_12 | HAL_GPIO_PIN_1 | HAL_GPIO_PIN_0, &gpio_config);


  /**
    OCTOSPIM GPIO Configuration
    PC1   --------->  OCTOSPIM_P1_IO4
    PC2   --------->  OCTOSPIM_P1_IO5
    PC3   --------->  OCTOSPIM_P1_IO6
    **/
  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_10;
  HAL_GPIO_Init(HAL_GPIOC, HAL_GPIO_PIN_1 | HAL_GPIO_PIN_2 | HAL_GPIO_PIN_3, &gpio_config);


  /**
    OCTOSPIM GPIO Configuration
    PC0   --------->  OCTOSPIM_P1_IO7
    **/
  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_3;
  HAL_GPIO_Init(HAL_GPIOC, HAL_GPIO_PIN_0, &gpio_config);

  return SYSTEM_OK;
}

Deinitialization sequence

static void MX_XSPI1_DeInit(void)
{
  HAL_QSPI_DeInit(&hxspi);
}




void HAL_QSPI_MspDeInit(QSPI_HandleTypeDef* hqspi)
{
  if(hqspi->Instance==QUADSPI)
  {

    /* Peripheral clock disable */
    __HAL_RCC_QSPI_CLK_DISABLE();

    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0|GPIO_PIN_1);
    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_6|GPIO_PIN_7);
  }
}
void mx_xspi1_deinit(void)
{
  (void)HAL_XSPI_DeInit(&hOCTOSPI1);
  HAL_RCC_OCTOSPI1_Reset();
  HAL_RCC_OCTOSPI1_DisableClock();
}

void mx_xspim_deinit(void)
{
  HAL_RCC_OCTOSPIM_Reset();
  HAL_RCC_OCTOSPIM_DisableClock();
}